package com.gym.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gym.dao.MemberDao;
import com.gym.pojo.Catsaletake;
import com.gym.pojo.Cattype;
import com.gym.pojo.Member;
import com.gym.pojo.User;
import com.gym.service.MemberService;
import com.gym.utils.Different;
import com.gym.vo.CatsaletakeVo;
import com.gym.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberDao memberDao;

    @Override
    public List<UserVo> listMem(String name,int catId) {
        return memberDao.listMem(name,catId);
    }

    @Override
    public PageInfo<UserVo> selListMember(String name, int catId, int index, int size) {
        PageHelper.startPage(index, size);
        PageInfo<UserVo> page=new PageInfo<UserVo>(memberDao.listMem(name,catId),5);
        return page;
    }

    @Override
    public List<UserVo> expireMem(String date, String time) {
        return memberDao.expireMem(date,time);
    }

    @Override
    public boolean updMemStatic(int memId) {
        return memberDao.updMemStatic(memId)>0;
    }

    @Override
    public List<Cattype> listCat(String name) {
        return memberDao.listCat( name);
    }

    @Override
    public boolean delCatById(int catId) {
        return memberDao.delCatById(catId);
    }

    @Override
    public boolean selCatByMem(int catId) {
        return memberDao.selCatByMem(catId)>0;
    }

    @Override
    public boolean updCatById(Cattype cat) {
        return memberDao.updCatById(cat)>0;
    }

    @Override
    public boolean insCat(Cattype cat) {
        return memberDao.insCat(cat)>0;
    }

    @Override
    public boolean saveMem(Member member) {
        return memberDao.saveMem(member)>0;
    }

    @Override
    public boolean saveCst(Catsaletake cst) {
        return memberDao.saveCst(cst)>0;
    }

    @Override
    public PageInfo<CatsaletakeVo> selCatSaleTake(String date,String time, int index, int size) throws ParseException {
        PageHelper.startPage(index,size);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        if (date!=null&&date!=""&&!date.equals("")){
            date=sdf.format(sdf.parse(date));
            System.out.println(date);
        }
        if (time!=null&&time!=""&&!time.equals("")){
            time=sdf.format(sdf.parse(time));
        }
        PageInfo<CatsaletakeVo> page=new PageInfo<CatsaletakeVo>
                (memberDao.selCatSaleTake(date,time),5);
        return page;
    }

    @Override
    public UserVo selUserById(int userId) {
        return memberDao.selUserById(userId);
    }

    @Override
    public boolean updMemExpires(Member mem) {
        return memberDao.updMemExpires(mem)>0;
    }

    @Override
    public Date selSaleDate(int userId) {
        return memberDao.selSaleDate(userId);
    }

    @Override
    @Transactional
    public boolean continuous(Catsaletake cst,Member mem,Cattype cat) {
        Date d=new Date();
        Date date1=selSaleDate(cst.getUserId());
        int  expires= Different.differentDays(date1,mem.getExpires());//续会员之前还有多少天
        Date date2=Different.getDateAfter(d,expires+cat.getValiddays()+1);//
        mem.setUserId(cst.getUserId());//更新条件
        mem.setExpires(date2);//放入需要更新的时间
        mem.setCattype(cat.getCatId());
        cst.setSaledate(d);
        boolean b=memberDao.saveCst(cst)>0;
        boolean b2=memberDao.updMemExpires(mem)>0;
        if (b&&b2){
            return true;
        }
        return false;
    }
}
